/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package practica1compi;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import javax.swing.JPanel;
import org.nfunk.jep.type.Complex;

/**
 *
 * @author Ebert
 */
class ZonaGrafica extends JPanel  implements MouseListener, MouseMotionListener 
 {
    int     offsetX, offsetY;   
    boolean dragging; 
    VentanaResultados principal;
    ZonaGrafica(VentanaResultados tem) 
    {    
        principal=tem;
    setBackground(Color.white);             
         offsetX=principal.x0; offsetY=principal.y0;        
         addMouseListener(this);
         addMouseMotionListener(this);
    }
    
    
     public void mousePressed(MouseEvent evt) 
     {  
                                     
         if (dragging)  
            return;
         int x = evt.getX();  // clic inicial
         int y = evt.getY();
         offsetX = x - principal.x0;  
         offsetY = y - principal.y0;
         dragging = true;                     
      }


      public void mouseReleased(MouseEvent evt) 
      {                         
          dragging = false;        
          repaint();          
      }


      public void mouseDragged(MouseEvent evt) 
      {               
          if (dragging == false)  
            return;

          int x = evt.getX();   // posición del mouse
          int y = evt.getY();
          principal.x0 = x - offsetX;     // mover origen
          principal.y0 = y - offsetY;    
                               
          repaint();
      }

           //el resto hace nada 
      public void mouseMoved(MouseEvent evt) {}           
      public void mouseClicked(MouseEvent evt) { }
      public void mouseEntered(MouseEvent evt) { }
      public void mouseExited(MouseEvent evt) { }            

 
  public void paintComponent(Graphics g) 
  {                   
         super.paintComponent(g);       // clear to background color  
                                          	 
         Graficar(g, principal.x0, principal.y0);                      
                        
         
 }
 
 void Graficar(Graphics ap, int xg, int yg)
 {
  int xi=0,yi=0,xi1=0,yi1=0,numPuntos=1;
  int cxmin,cxmax,cymin,cymax;
  double valxi=0.0, valxi1=0.0, valyi=0.0,valyi1=0.0; 

  //convertimos el objeto ap en un objeto Graphics2D para usar los métodos Java2D
  Graphics2D g = (Graphics2D) ap;
  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

  g.setFont(principal.ft10); 
  g.setPaint(new Color(0,0,150)); 

  //eje Y
 g.draw(new Line2D.Double(xg, 10, xg, principal.Galto-10)); 
  //eje X
  g.draw(new Line2D.Double(10, yg, principal.Gancho-10, yg));

  principal.xmin=-1.0*xg/principal.escalaX;
  principal.xmax=(1.0*(principal.Gancho-xg)/principal.escalaX);
  cxmin=(int)Math.round(principal.xmin); //pantalla
  cxmax=(int)Math.round(principal.xmax);
  cymin=(int)Math.round(1.0*(yg-principal.Galto)/principal.escalaY);
  cymax=(int)Math.round(1.0*yg/principal.escalaY);

  numPuntos=principal.Gancho; //num pixels

  g.setPaint(Color.gray); 
  g.setFont(principal.ft7);

  //marcas en los ejes (ticks)
  if(principal.escalaX>5)
  {
    for(int i=cxmin+1;i<cxmax;i++)
    {  g.draw(new Line2D.Double(xg+i*principal.escalaX, yg-2, xg+i*principal.escalaX , yg+2));
       if(i>0)
          g.drawString(""+i, xg+i*principal.escalaX-2, yg+12);
       if(i<0)
         g.drawString(""+i, xg+i*principal.escalaX-6, yg+12); 
    }
  }

  if(principal.escalaY>5)
  { 
    for(int i=cymin+1;i<cymax;i++)
    {  g.draw(new Line2D.Double(xg-2, yg-i*principal.escalaY, xg+2 , yg-i*principal.escalaY));  
       if(i>0)
          g.drawString(""+i, xg-12,yg-i*principal.escalaY+3 );
       if(i<0)
         g.drawString(""+i, xg-14,yg-i*principal.escalaY+3 );
   }
  }
  g.setPaint(new Color(50,100,0));

  g.setStroke(principal.grosor1);
  
   principal.miEvaluador.parseExpression(principal.texto);
   principal.errorEnExpresion = principal.miEvaluador.hasError(); //hay error?  

  if(!principal.errorEnExpresion)
  { 
   
    

   for(int i=0;i<numPuntos-1;i++)
   {
     valxi =principal.xmin +i*1.0/principal.escalaX;
     valxi1=principal.xmin+(i+1)*1.0/principal.escalaX;
     principal.miEvaluador.addVariable("x", valxi);
     valyi=principal.miEvaluador.getValue();  
     principal.miEvaluador.addVariable("x", valxi1);
     valyi1 = principal. miEvaluador.getValue();    
     xi =(int)Math.round(principal.escalaX*(valxi));
     yi =(int)Math.round(principal.escalaY*valyi); 
     xi1=(int)Math.round(principal.escalaX*(valxi1));
     yi1=(int)Math.round(principal.escalaY*valyi1); 

     //control de discontinuidades groseras y complejos
     Complex valC = principal.miEvaluador.getComplexValue();
     double imgx = (double)Math.abs(valC.im()); 
     if(dist(valxi,valyi,valxi1,valyi1)< 1000 && imgx==0) 
     { 
       g.draw(new Line2D.Double(xg+xi,yg-yi,xg+xi1,yg-yi1)); 
     } 
    }//fin del for 
   }else{
        
  } 
 }//Graficar
 
  double dist(double xA,double yA, double xB,double yB)
  {
			   return (xA - xB)*(xA - xB)+(yA - yB)*(yA - yB);
  }//
 
 } // class
